function e(e, o) {
    if (!(e instanceof o)) throw new TypeError("Cannot call a class as a function");
}

var o = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
} : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
}, t = function() {
    function e(e, o) {
        for (var t = 0; t < o.length; t++) {
            var n = o[t];
            n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), 
            Object.defineProperty(e, n.key, n);
        }
    }
    return function(o, t, n) {
        return t && e(o.prototype, t), n && e(o, n), o;
    };
}(), n = require("./config"), s = n.custom.api, i = n.custom.key, a = n.id, r = "https://cloud.safedog.cc/vpush", c = function() {
    function n() {
        if (e(this, n), !i && !a) throw console.warn("[VPUSH_ERROR] 请编辑vpush/config.js文件，加入您的应用ID或者接口配置！"), 
        new Error("[VPUSH_INIT]");
        this.openId = "", this.TAGS = [], this.ALIAS = "";
        var o = wx.getSystemInfoSync();
        this.INFO = {
            sdk: o.SDKVersion,
            language: o.language,
            model: o.model,
            platform: o.platform,
            system: o.system,
            version: o.version
        };
        try {
            var t = wx.getStorageSync("VPUSH_OPEN_ID");
            t && (this.openId = t);
        } catch (e) {}
        this.init();
    }
    return t(n, [ {
        key: "init",
        value: function() {
            var e = this;
            if (this.openId) return console.log("[init.has-openid]", this.openId);
            wx.login({
                success: function(o) {
                    wx.request({
                        url: s ? s + "/getOpenId" : r + "/functions/getOpenId",
                        method: "POST",
                        header: {
                            "Content-Type": "application/json",
                            "X-Parse-Application-Id": i || "guren_cloud_vpush"
                        },
                        dataType: "json",
                        data: {
                            appId: a,
                            code: o.code
                        },
                        success: function(o) {
                            if (console.log("[vpush.init]", o), s) t = o.data.openId; else var t = o.data.result.data.openid;
                            t && (e.openId = t, wx.setStorageSync("VPUSH_OPEN_ID", t));
                        }
                    });
                }
            });
        }
    }, {
        key: "add",
        value: function(e) {
            var t = "";
            t = "object" === (void 0 === e ? "undefined" : o(e)) ? e.detail.formId : String(e), 
            this._create(t, function(e) {
                console.log("[vpush.add.result]", e);
            });
        }
    }, {
        key: "_create",
        value: function(e, o) {
            return this.openId ? e.startsWith("the") ? console.warn("[vpush.formId]", e) : void wx.request({
                url: s ? s + "/vpush/classes/FormIds" : r + "/classes/FormIds",
                method: "POST",
                header: {
                    "X-Parse-Application-Id": i || "guren_cloud_vpush",
                    "Content-Type": "application/json"
                },
                dataType: "json",
                data: Object.assign({}, this.INFO, {
                    formId: e,
                    openId: this.openId,
                    tags: this.TAGS,
                    alias: this.ALIAS
                }, s ? {} : {
                    app: {
                        __type: "Pointer",
                        className: "Apps",
                        objectId: a
                    }
                }),
                success: function(e) {
                    console.log("[vpush.create.success]", e), o && o(!0);
                },
                fail: function(e) {
                    console.warn("[vpush.create.fail]", e), o && o(!1);
                }
            }) : console.warn("[vpush.create.no-openid]");
        }
    }, {
        key: "setAlias",
        value: function(e) {
            this.ALIAS = e;
        }
    }, {
        key: "setTags",
        value: function(e) {
            if ("string" == typeof e) this.TAGS = [ e ]; else {
                if (!Array.isArray(e)) throw new Error("tag 应为string或array类型！");
                this.TAGS = e;
            }
        }
    }, {
        key: "pushToMe",
        value: function(e, o) {
            wx.request({
                url: r + "/functions/PUSH_API",
                method: "POST",
                header: {
                    "X-Parse-Application-Id": "guren_cloud_vpush",
                    "Content-Type": "application/json"
                },
                dataType: "json",
                data: Object.assign(e, {
                    openId: this.openId
                }),
                success: function(e) {
                    console.log("[pushToMe.success]", e), o && o(e);
                },
                fail: function(e) {
                    console.log("[pushToMe.failed]", e), o && o(!1);
                }
            });
        }
    } ]), n;
}();

module.exports = new c();